package net.towee.client.com.stuff.presenter;

import net.towee.client.com.stuff.GroupsCreator;
import net.towee.client.com.stuff.GroupsFinder;
import net.towee.client.com.stuff.Stuff;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

public class StuffPresenter implements Stuff, ClickHandler {

	public interface Display extends IsWidget {
		HasClickHandlers getFinderTab();
		
		HasClickHandlers getCreatorTab();
		
		HasWidgets getTabContainer();
	}

	// Injected
	protected final Display display;
	protected final GroupsCreator creator;
	protected final GroupsFinder finder;
	
	@Inject
	public StuffPresenter(Display display, GroupsFinder finder, GroupsCreator creator) {
		this.finder = finder;
		this.creator = creator;
		
		this.display = display;
		
		display.getFinderTab().addClickHandler(this);
		display.getCreatorTab().addClickHandler(this);
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add( display.asWidget() );
		
		// Show finder tab
		display.getTabContainer().clear();
		finder.go( display.getTabContainer() );
	}

	@Override
	public void onClick(ClickEvent event) {
		display.getTabContainer().clear();
		Object source = event.getSource();
		
		if(source.equals( display.getFinderTab() )) {
			finder.go( display.getTabContainer() );
		}	else if(source.equals( display.getCreatorTab() )) {
			creator.go( display.getTabContainer() );
		}
	}
}